package com.screwdriver.jso.processor.utils;

import com.sun.mirror.declaration.MethodDeclaration;
import com.sun.mirror.type.TypeMirror;
import com.sun.mirror.type.VoidType;

/**
 * Created by IntelliJ IDEA.
 * User: jan kowalski
 * Date: 08.08.11
 * Time: 22:29
 * To change this template use File | Settings | File Templates.
 */
public class PropertyUtil {

    private PropertyUtil(){

    }
	
    public static boolean isPropertyMethod(MethodDeclaration md) {
        if (isSetter(md)) {
            return true;
        } else if (isGetter(md)) {
            return true;
        }
        return false;
    }

    public static boolean isGetter(MethodDeclaration md) {
        return md.getParameters().size() == 0 && !(md.getReturnType() instanceof VoidType)
                && (md.getSimpleName().startsWith("is") || md.getSimpleName().startsWith("get"));
    }

    public static boolean isSetter(MethodDeclaration md) {
        return md.getSimpleName().startsWith("set") && md.getParameters().size() == 1
                && md.getReturnType() instanceof VoidType;
    }

    public static String getPropertyName(MethodDeclaration md) {
        String rawPropertyName = null;
        if (isSetter(md)) {
            rawPropertyName = md.getSimpleName().substring(3);
        } else if (isGetter(md)) {
            if (md.getSimpleName().startsWith("is")) {
                rawPropertyName = md.getSimpleName().substring(2);
            } else {
                rawPropertyName = md.getSimpleName().substring(3);
            }
        }
        return rawPropertyName.substring(0, 1).toLowerCase() + rawPropertyName.substring(1);
    }

	public static TypeMirror getPropertyType(MethodDeclaration md) {
		if (isGetter(md)) {
			return md.getReturnType();
		} else if (isSetter(md)) {
			return md.getParameters().iterator().next().getType();
		}
		return null;
	}

}
